package com.mybatisflex.test.codegen;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.core.service.IService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.zaxxer.hikari.HikariDataSource;

/**
 * @author 公众号: SimpleMemory
 * @version 1.0.0
 * @ClassName Codegen.java
 * @Description 代码生成器
 * @createTime 2023年09月21日 18:12:00
 */
public class CodegenMain {

    public static void main(String[] args) {
        //数据源配置
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/ballcat?characterEncoding=utf-8");
        dataSource.setUsername("xxkfz");
        dataSource.setPassword("xxkfz");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfig();
        //GlobalConfig globalConfig = createGlobalConfigUseStyle2();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }


    /**
     * GlobalConfig 基本配置
     *
     * @return
     */
    public static GlobalConfig createGlobalConfig() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.setBasePackage("com.xxkfz.simplememory");

        //设置表前缀和只生成哪些表
        globalConfig.setGenerateTable("user_order");

        //设置生成 entity 并启用 Lombok
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);


        //设置生成 controller
        globalConfig.setControllerGenerateEnable(true);
        //设置生成 service
        globalConfig.setServiceGenerateEnable(true);
        //设置生成 serviceImpl
        globalConfig.setServiceImplGenerateEnable(true);
        //设置生成 mapper
        globalConfig.setMapperGenerateEnable(true);
        //设置生成 mapperXml
        globalConfig.setMapperXmlGenerateEnable(true);


        globalConfig.getControllerConfig().setClassPrefix("").setClassSuffix("Controller").setRestStyle(true).setOverwriteEnable(true);
        globalConfig.getServiceConfig().setClassPrefix("").setClassSuffix("Service").setSuperClass(IService.class).setOverwriteEnable(true);
        globalConfig.getServiceImplConfig().setClassPrefix("").setClassSuffix("ServiceImpl").setSuperClass(ServiceImpl.class).setOverwriteEnable(true);
        globalConfig.getMapperXmlConfig().setFilePrefix("/com/xxkfz/simplememory/mapper/").setFileSuffix("Mapper").setOverwriteEnable(true);

        //可以单独配置某个列
        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setColumnName("tenant_id");
        columnConfig.setLarge(true);
        columnConfig.setVersion(true);
        globalConfig.setColumnConfig("tb_account", columnConfig);

        globalConfig.getTableDefConfig().setOverwriteEnable(true);
        globalConfig.getJavadocConfig()
                .setAuthor("xxkfz");

        return globalConfig;
    }
}